package com.qf.index.gcrdo.ab;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONArray;
import com.qf.index.gcrdo.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Author: 葛成瑞
 * @Date: 2025/4/22 13:36
 */
@Slf4j
public class GetNoTransAbRecord {

    @SneakyThrows
    public static void main(String[] args) {

        // 此程序是用于遍历unified_search_summary_0406 表只要字段 cn_discussion cn_results cn_methods cn_background  有值 且翻译没有翻译的则记录下来

        // 遍历unified_search_summary_0406 表
        String table = "unified_search_summary_0406";
        String saveTable = "unified_search_no_trans_ab";
        Db use = Db.use();
        Integer maxId = use.query("select max(id) as maxid from " + table).get(0).getInt("maxid");
        System.out.println(maxId);
        Integer minId = 0;
        Integer step = 10000;
        for (int i = minId; i < maxId; i += step) {
            System.out.println("页码：" + i + " 到 " + (i + step) + " 开始查询");
            String sql = "select id,cn_discussion,cn_results,cn_methods,cn_background from " + table + " where id > " + i + " and id <= " + (i + step) + " ";
            List<Entity> query = use.query(sql);
            query.parallelStream().forEach(entity -> {
                Integer id = entity.getInt("id");
                String cn_discussion = entity.getStr("cn_discussion");
                String cn_results = entity.getStr("cn_results");
                String cn_methods = entity.getStr("cn_methods");
                String cn_background = entity.getStr("cn_background");
                boolean cn_discussionisIns = false;
                boolean cn_resultsisIns = false;
                boolean cn_methodsisIns = false;
                boolean cn_backgroundisIns = false;
                if (StringUtil.isNotNull(cn_discussion)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_discussion, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        if (!checkIsTrans(cn) || !checkIsTrans(en)) {
                            cn_discussionisIns = true;
                            break;
                        }
                    }
                }
                if (StringUtil.isNotNull(cn_results)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_results, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        if (!checkIsTrans(cn) || !checkIsTrans(en)) {
                            cn_resultsisIns = true;
                            break;
                        }
                    }
                }
                if (StringUtil.isNotNull(cn_methods)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_methods, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        if (!checkIsTrans(cn) || !checkIsTrans(en)) {
                            cn_methodsisIns = true;
                            break;
                        }
                    }
                }
                if (StringUtil.isNotNull(cn_background)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_background, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        if (!checkIsTrans(cn) || !checkIsTrans(en)) {
                            cn_backgroundisIns = true;
                            break;
                        }
                    }
                }

                if (cn_discussionisIns || cn_resultsisIns || cn_methodsisIns || cn_backgroundisIns) {
                    Entity ins = new Entity();
                    ins.set("id", id);
                    if (cn_discussionisIns) {
                        ins.set("cn_discussion", cn_discussion);
                    }
                    if (cn_resultsisIns) {
                        ins.set("cn_results", cn_results);
                    }
                    if (cn_methodsisIns) {
                        ins.set("cn_methods", cn_methods);
                    }
                    if (cn_backgroundisIns) {
                        ins.set("cn_background", cn_background);
                    }
                    ins.setTableName(saveTable);
                    try {
                        log.info("插入成功：" + id);
                        use.insert(ins);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("插入失败：" + id);
                    }

                }
            });
        }

    }

    public static boolean checkIsTrans(String en) {
        if (StringUtil.isNull(en)) {
            return false;
        }
        if (StringUtil.isNotNull(en)) {
            String ĺļ = en.replace("ĺļ", "");
            if (StringUtil.isNull(ĺļ)) {
                return false;
            }
        }
        return true;

    }
}
